using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing.Imaging;
using hedefgrup.graphicsLibrary;
using hedefgrup.reports.renault.date;
using System.Globalization;
using System.Drawing;

public partial class forms_common_graph3 : reportPage
{
    private static CultureInfo english = CultureInfo.GetCultureInfo("en-US");

    protected void Page_Load(object sender, EventArgs e)
    {
        base.initialize();
        base.ensureUserIsLoggedIn();

        reportMonth month = base.getReportEndMonth();
        string mapName = Request["g_n"];

        string[] yValuesAsString = base.getString("g_v").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        string[] y2ValuesAsString = base.getString("g_v2").Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

        float[] yValues = new float[yValuesAsString.Length];
        float[] y2Values = new float[y2ValuesAsString.Length];

        float yMaxValue = float.MinValue;
        float yMinValue = 50;

        for (int i = 0; i < yValues.Length; i++)
        {
            if (yValuesAsString[i].StartsWith(".")) yValuesAsString[i] = "0" + yValuesAsString[i];
            if (y2ValuesAsString[i].StartsWith(".")) y2ValuesAsString[i] = "0" + y2ValuesAsString[i];
            yValues[i] = Single.IsNaN(yValues[i] = Convert.ToSingle(yValuesAsString[i], english)) ? 0 : yValues[i];
            y2Values[i] = Single.IsNaN(y2Values[i] = Convert.ToSingle(y2ValuesAsString[i], english)) ? 0 : y2Values[i];

            yMaxValue = yMaxValue < yValues[i] ? yValues[i] : yMaxValue;
            yMaxValue = yMaxValue < y2Values[i] ? y2Values[i] : yMaxValue;

            yMinValue = yMinValue > yValues[i] ? 0 : yMinValue;
            yMinValue = yMinValue > y2Values[i] ? 0 : yMinValue;
        }

        if (yMaxValue > 50) yMaxValue = 100;

        if (yMaxValue < 5)
        {
            yMaxValue = 5;
        }
        else if (yMaxValue < 10)
        {
            yMaxValue = 10;
        }

        if (yMinValue < 50) yMinValue = 0;

        trendGraphic gr = new trendGraphic();

        gr.mapNameLines = new string[] { mapName };

        gr.yMinimumValue = Convert.ToInt32(yMinValue);
        gr.yMaximumValue = Convert.ToInt32(Math.Ceiling(yMaxValue));

        gr.yValues = yValues;
        gr.y2Values = y2Values;

        string[] xLabels = new string[12];

        for (int i = 11; i >= 0; i--) xLabels[11 - i] = string.Format("{0}|{1}", (month - i).getMonthName(), (month - i).getYear());

        gr.xLabels = xLabels;

        gr.mapNameFont = new Font("tahoma", 10F, FontStyle.Bold);
        gr.xLabelFont = new Font("verdana", 7F, FontStyle.Regular);
        gr.yLabelFont = new Font("verdana", 7F, FontStyle.Regular);

        Response.ContentType = "image/jpeg";
        gr.WriteImage(Response.OutputStream, ImageFormat.Jpeg);
    }
}
